//
//  GetAnAccountView.swift
//  SwiftForwardWayZhe
//
//  Created by 刘喆 on 2022/1/7.
//

import UIKit
import SwiftUI
import CarPlay

class GetAnAccountView: BaseView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor(white: 0, alpha: 0.6)
        
        self.setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        self.addSubview(containerView)
        
        containerView.addSubview(self.topImageView)
        containerView.addSubview(self.titleLabel)
        containerView.addSubview(self.messageLabel)
        containerView.addSubview(self.lineView)
        containerView.addSubview(self.closeButton)
        containerView.addSubview(self.knowBtn)
        
        self.titleLabel.text = "如何获得登录账号"
        self.messageLabel.text = "视力筛查登录账号由负责学校筛查的“筛查单位后台”进行账号分配，请联系您的筛查单位帮您分配登录账号。\n\n注：广西壮族自治区学生普查，请选择广西，其他地区或单位请选择全国。"

        self.makeConstrains()
        
        
    }
    
    
    func makeConstrains() {
        
        containerView.snp.makeConstraints{ make in
            make.width.equalTo(280)
            make.height.equalTo(355)
            make.centerY.equalTo(self)
            make.centerX.equalTo(self)
        }
        
        topImageView.snp.makeConstraints{ make in
            make.centerX.equalTo(self.containerView)
            make.top.equalTo(self.containerView).offset(22.5)
            make.height.equalTo(70.5)
            make.width.equalTo(82.5)
        }
        
        titleLabel.snp.makeConstraints{ make in
            make.top.equalTo(self.topImageView.snp.bottom).offset(18)
            make.centerX.equalTo(self.containerView)//6
        }
        
        messageLabel.snp.makeConstraints{ make in
            make.top.equalTo(self.titleLabel.snp.bottom).offset(20);
            make.left.equalTo(self.containerView).offset(19);
            make.right.equalTo(self.containerView).offset(-19);
        }
        
        lineView.snp.makeConstraints{ make in
            make.height.equalTo(1);
            make.left.equalTo(self.containerView).offset(15);
            make.right.equalTo(self.containerView).offset(-15);
            make.top.equalTo(self.messageLabel.snp.bottom).offset(20);
        }
        
        closeButton.snp.makeConstraints{ make in
            make.top.equalTo(self.containerView).offset(15);
            make.right.equalTo(self.containerView).offset(-15);
            make.width.height.equalTo(16);
        }
        
        knowBtn.snp.makeConstraints{ make in
            make.top.equalTo(self.lineView.snp.bottom).offset(8)
            make.left.right.equalTo(0)
            make.height.equalTo(44)
        }
    }
    
    lazy var containerView: UIView = {
        let containerView = UIView.init()
        containerView.backgroundColor = UIColor.white
        containerView.layer.cornerRadius = 10
        return containerView
   }()
    
    lazy var topImageView: UIImageView = {
        let topImageView = UIImageView.init()
        topImageView.image = UIImage.init(named: "alert_pop_icon_loudSpeaker")
        topImageView.contentMode = .scaleAspectFill
        return topImageView
   }()
    
    lazy var titleLabel: UILabel = {
        let titleLabel = UILabel.init()
        titleLabel.numberOfLines = 1
        titleLabel.font = UIFont.init(name: kFontPingfangMedium, size: 19)
        titleLabel.textColor = kAPPdarkGrayColor
        titleLabel.textAlignment = .center
        return titleLabel
   }()
    
    lazy var messageLabel: UILabel = {
        let messageLabel = UILabel.init()
        messageLabel.numberOfLines = 0
        messageLabel.font = UIFont.init(name: kFontPingfangRegular, size: 14)
        messageLabel.textColor = UIColor.init(_colorLiteralRed: 51 / 255.0, green: 51 / 255.0, blue: 51 / 255.0, alpha: 1)
        messageLabel.lineBreakMode = .byCharWrapping
        messageLabel.textAlignment = .center
        return messageLabel
   }()
    
    
    lazy var lineView: UIView = {
        let lineView = UIView.init()
        lineView.backgroundColor = kRGBColorFromHex(rgbValue: 0xf2f2f2)
        return lineView
   }()
    
    lazy var closeButton: UIButton = {
        let closeButton = UIButton.init(type:.custom)
        closeButton.tintColor = UIColor.lightGray
        closeButton.setImage(UIImage.init(named: "eyeTestClose"), for: .normal)
        closeButton.addTarget(self, action: #selector(dissmiss), for: UIControl.Event.touchUpInside)
        return closeButton
   }()
    
    lazy var knowBtn : UIButton = {
        
        let knowBtn = UIButton.init(type:.custom)
        knowBtn.setTitle("我知道了", for: UIControl.State.normal)
        knowBtn.setTitleColor(kAppStyleColor, for: UIControl.State.normal)
        knowBtn.titleLabel?.font = .systemFont(ofSize: 18)
        knowBtn.addTarget(self, action: #selector(dissmiss), for: UIControl.Event.touchUpInside)
        
        return knowBtn
    }()
    
    
    
    //MARK: - 显示/隐藏
    public func show() -> Void {
        UIApplication.shared.keyWindow?.addSubview(self)
        
        let animation = CABasicAnimation.init(keyPath: "transform.scale")
        animation.fromValue = 0.8
        animation.toValue = 1
        animation.duration = 0.15
        animation.autoreverses = false
        animation.repeatCount = 1
        animation.isRemovedOnCompletion = true
        animation.fillMode = .forwards
        self.containerView.layer.add(animation, forKey: "animation")

    }
    
    
    @objc private func dissmiss() -> Void {
        UIView.animate(withDuration: 0.15) {
            self.containerView.layer.transform = CATransform3DMakeScale(0.5, 0.5, 1)
            self.containerView.alpha = 0;
        } completion: { _ in
            self.isHidden = true
            self.removeFromSuperview()
        }
    }



}
